package com.coolgor.xcode;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class XCoder {
//    private static String basePath = "../..";

    private static String runtimePath() {
        File f = new File("");
        String cf = null;
        try {
            cf = f.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cf;
    }

    private static List<String> getFileList(String path){
        List<String> result = new ArrayList<String>();
        File file = new File(path);
        if (file.exists()) {//判断文件目录的存在
            if(file.isDirectory()){//判断文件的存在性
                //是一个文件夹，则读取文件夹下所有文件的路径
                File[] files = file.listFiles();
                for (File f : files) {
                    if (!f.isDirectory()) {
                        result.add(f.getPath());
                    }
                }

            }else{
                result.add(path);
            }
        } else {
            return null ;
        }
        return result;
    }

    private static List<String[]> readFile(String fileName){
        BufferedReader reader;
        List<String[]> result = new ArrayList<String[]>();
        try {
            reader = new BufferedReader(new FileReader(fileName));
            String line ;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                result.add(line.strip().replaceAll("\\s{1,}", " ").split(" "));
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }



    public static void stack() {
        String basePath = XCoder.runtimePath()+"/src/XCoder";
        List<String> fileList  = XCoder.getFileList(basePath);
        if (fileList !=null)
            fileList.forEach(filePath -> {
                System.out.println(filePath);
                List<String[]> context = XCoder.readFile(filePath);
                if ( null!=context && context.size()>1 ){
                    try {
                        StackEntity.run(basePath,context);
                        StackDao.run(basePath,context);
                        StackDto.run(basePath,context);
                        StackOperationStateEnum.run(basePath,context);
                        StackOperationException.run(basePath,context);
                        StackService.run(context,
                                        basePath,
                                "com.coolgor.coldot",
                                "wechat.gzh");
                        StackServiceImpl.run(context,
                                            basePath,
                                            "com.coolgor.coldot",
                                            "wechat.gzh");
                        StackController.run(context,
                                            basePath,
                                "com.coolgor.coldot",
                                "wechat.gzh");
                    } catch (IOException ioe){
                        ioe.printStackTrace();
                    }
                }
            });
        else
            System.out.println("文件列表为空！");
    }

    public static void main(String[] args) {
        XCoder.stack();
    }
}
